package com.itboye.textileexpres.top;

import java.util.Observable;
import java.util.Observer;

import com.itboye.tbshopcar.listener.UIAlertView;
import com.itboye.textileexpes.beans.UserBean;
import com.itboye.textileexpes.presenter.MainPresenter;
import com.itboye.textileexpres.base.BaseActivity;
import com.itboye.textileexpres.base.MainApplication;
import com.itboye.textileexpres.base.ResultEntity;
import com.itboye.textileexpres.base.SpPublicParams;
import com.itboye.textileexpres.main.MainActivity;
import com.itboye.textileexpres.util.MyToast;
import com.itboye.textileexpres.util.SPUtils;
import com.itboye.textileexptes.config.GlobalConfig;

import android.app.Service;
import android.content.Intent;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.IBinder;
import android.os.Looper;

public class MyService extends Service implements Observer {
	String uid;
	MainPresenter mainPresenter;
	public static final String EVENT_TYPE_UNKNOWN = BaseActivity.class
			.getName() + "_unknown";
	UIAlertView delDialog;

	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void onCreate() {
		// TODO Auto-generated method stub
		super.onCreate();
		mainPresenter = new MainPresenter(this);

	}

	final Handler handler = new Handler();
	Runnable thread = new Runnable() {

		@Override
		public void run() {
			uid = (String) SPUtils.get(MainApplication.getContext(), null,
					SpPublicParams.UID, "");
			if (uid == null || "".equals(uid) || uid == "") {

			} else {
				UserBean bean = new UserBean();
				bean.setUid(uid);
				bean.setAutoLoginCode((String) SPUtils.get(
						MainApplication.context, null,
						SpPublicParams.Auto_Code, ""));
				mainPresenter.Autologin(bean);
			}

			handler.postDelayed(this, 6000);
		}
	};

	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		// TODO Auto-generated method stub
		handler.postDelayed(thread, 6000);
		return super.onStartCommand(intent, flags, startId);

	}

	@Override
	public boolean onUnbind(Intent intent) {
		// TODO Auto-generated method stub
		return super.onUnbind(intent);
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		handler.removeCallbacks(thread);
	}

	public ResultEntity handlerError(Object data) {
		ResultEntity result = (ResultEntity) data;
		if (result == null) {
			result = new ResultEntity("-1", "数据格式错误!~", data);
			return result;
		}

		if (result.hasError()) {
			MyToast.toast(result.getMsg());
			result.setEventType(EVENT_TYPE_UNKNOWN);
			return result;
		}

		return result;
	}

	@Override
	public void update(Observable observable, Object data) {
		// TODO Auto-generated method stub
		ResultEntity entity = handlerError(data);
		if (entity != null) {
			if (entity.getEventType() == MainPresenter.AutoLoginSuccess) {
				AutoLoginSuccess(entity);
			} else if (entity.getEventType() == MainPresenter.AutoLoginFail
					|| entity.getEventType() == EVENT_TYPE_UNKNOWN) {
				MyToast.toast(entity.getMsg() + "");
				SPUtils.put(MainApplication.context, null,
						SpPublicParams.USER_NAME, "");
				SPUtils.put(MainApplication.context, null,
						SpPublicParams.USERHEAD, "");
				SPUtils.put(MainApplication.context, null,
						SpPublicParams.NICK_NAME, "");
				SPUtils.put(MainApplication.context, null, SpPublicParams.UID,
						"");
				SPUtils.put(MainApplication.context, null, SpPublicParams.SEX,
						"");
				SPUtils.put(MainApplication.context, null,
						SpPublicParams.SIGNATURE, "");
				SPUtils.put(MainApplication.context, null,
						SpPublicParams.PHONE_MODEL, "");
				SPUtils.put(MainApplication.context, null, SpPublicParams.URL,
						"");
				Intent intent = new Intent("newshead");
				sendBroadcast(intent);
				Intent intent3 = new Intent(MainApplication.context,
						MainActivity.class);
				intent3.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
				MainApplication.context.startActivity(intent3);
				Intent intent2 = new Intent("gotoshopcart");
				intent2.putExtra("index", 4);
				sendBroadcast(intent2);
				//
				// delDialog = new UIAlertView(MainApplication.context, "", "",
				// "返回", "重新登陆");
				// delDialog.show();
				// delDialog
				// .setClicklistener(new UIAlertView.ClickListenerInterface() {
				//
				// public void doLeft() {
				// Intent intent = new Intent(
				// MainApplication.context,
				// MainActivity.class);
				// startActivity(intent);
				// delDialog.dismiss();
				// }
				//
				// public void doRight() {
				// Intent intent = new Intent(
				// MainApplication.context,
				// MainActivity.class);
				// startActivity(intent);
				// Intent intent2 = new Intent("gotoshopcart");
				// intent2.putExtra("index", 4);
				// sendBroadcast(intent2);
				// delDialog.dismiss();
				// }
				// });
			}
		}
	}

	void AutoLoginSuccess(ResultEntity entity) {
		// LoginController.setLoginState(new LoginedState());
		UserBean bean = (UserBean) entity.getData();
		SPUtils.put(MainApplication.context, null, SpPublicParams.UID,
				bean.getUid() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.USER_NAME,
				bean.getUsername() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.PASSWORD,
				bean.getPassword() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.NICK_NAME,
				bean.getNickname() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.PHONE_MODEL,
				bean.getMobile() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.realname,
				bean.getRealname() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD,
				bean.getHead());
		SPUtils.put(MainApplication.context, null, SpPublicParams.SEX,
				bean.getSex() + "");
		SPUtils.put(MainApplication.context, null,
				SpPublicParams.AcceptAddress, bean.getDefaultAddress() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.Company,
				bean.getCompany() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.Job,
				bean.getJobTitle() + "");
		SPUtils.put(MainApplication.context, null,
				SpPublicParams.AcceptAddress, bean.getLocArea() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.USERHEAD,
				bean.getHead() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.WX,
				bean.getWeixin() + "");
		SPUtils.put(MainApplication.context, null, SpPublicParams.IS_LOGINED,
				"1");
		SPUtils.put(MainApplication.context, null, SpPublicParams.Auto_Code,
				bean.getAutoLoginCode() + "");
		String time = String.valueOf((int) (Math.random() * 10));
		SPUtils.put(MainApplication.context, null, SpPublicParams.URL,
				GlobalConfig.IMAGE_HEAD + bean.getUid() + "&&random" + time);
	}
}
